#include "interface.h"
#include "graph.h"

GtkWidget*
create_font_select (GtkWidget *graph)
{
  GtkWidget *font_select;
  GtkWidget *vbox1;
  GtkWidget *fontselection1;
  GtkWidget *hbuttonbox1;
  GtkWidget *button1;
  GtkWidget *button2;
  GtkWidget *alignment1;
  GtkWidget *hbox1;
  GtkWidget *image1;
  GtkWidget *label1;
  GtkWidget *button3;

  font_select = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_name (font_select, "font_select");
  gtk_window_set_title (GTK_WINDOW (font_select), "w3live - Font Selection");
  gtk_window_set_position (GTK_WINDOW (font_select), GTK_WIN_POS_MOUSE);
  gtk_window_set_destroy_with_parent (GTK_WINDOW (font_select), TRUE);

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_widget_set_name (vbox1, "vbox1");
  gtk_widget_show (vbox1);
  gtk_container_add (GTK_CONTAINER (font_select), vbox1);

  fontselection1 = gtk_font_selection_new ();
  gtk_widget_set_name (fontselection1, "fontselection1");
  gtk_widget_show (fontselection1);
  gtk_box_pack_start (GTK_BOX (vbox1), fontselection1, TRUE, TRUE, 0);
  gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (fontselection1),
                                       "Pack my box with five dozen liquor jugs.");

  hbuttonbox1 = gtk_hbutton_box_new ();
  gtk_widget_set_name (hbuttonbox1, "hbuttonbox1");
  gtk_widget_show (hbuttonbox1);
  gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox1, TRUE, TRUE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (hbuttonbox1), 10);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox1), GTK_BUTTONBOX_END);
  gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox1), 10);

  button1 = gtk_button_new_from_stock ("gtk-cancel");
  gtk_widget_set_name (button1, "button1");
  gtk_widget_show (button1);
  gtk_container_add (GTK_CONTAINER (hbuttonbox1), button1);
  GTK_WIDGET_SET_FLAGS (button1, GTK_CAN_DEFAULT);

  button2 = gtk_button_new ();
  gtk_widget_set_name (button2, "button2");
  gtk_widget_show (button2);
  gtk_container_add (GTK_CONTAINER (hbuttonbox1), button2);
  GTK_WIDGET_SET_FLAGS (button2, GTK_CAN_DEFAULT);

  alignment1 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_set_name (alignment1, "alignment1");
  gtk_widget_show (alignment1);
  gtk_container_add (GTK_CONTAINER (button2), alignment1);

  hbox1 = gtk_hbox_new (FALSE, 2);
  gtk_widget_set_name (hbox1, "hbox1");
  gtk_widget_show (hbox1);
  gtk_container_add (GTK_CONTAINER (alignment1), hbox1);

  image1 = gtk_image_new_from_stock ("gtk-no", GTK_ICON_SIZE_BUTTON);
  gtk_widget_set_name (image1, "image1");
  gtk_widget_show (image1);
  gtk_box_pack_start (GTK_BOX (hbox1), image1, FALSE, FALSE, 0);

  label1 = gtk_label_new_with_mnemonic ("No Labels");
  gtk_widget_set_name (label1, "label1");
  gtk_widget_show (label1);
  gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (label1), GTK_JUSTIFY_LEFT);

  button3 = gtk_button_new_from_stock ("gtk-ok");
  gtk_widget_set_name (button3, "button3");
  gtk_widget_show (button3);
  gtk_container_add (GTK_CONTAINER (hbuttonbox1), button3);
  GTK_WIDGET_SET_FLAGS (button3, GTK_CAN_DEFAULT);

  gtk_signal_connect (GTK_OBJECT (font_select), "destroy",
                      GTK_SIGNAL_FUNC (gtk_widget_destroy),
                      NULL);
 
  gtk_signal_connect_object (GTK_OBJECT (button1), "clicked",
                      GTK_SIGNAL_FUNC (gtk_widget_destroy),
                      GTK_OBJECT (font_select));
 
  gtk_signal_connect (GTK_OBJECT (button2), "clicked",
                      GTK_SIGNAL_FUNC (unset_font),
                      (void *)font_select);

  gtk_signal_connect (GTK_OBJECT (button3), "clicked",
                      GTK_SIGNAL_FUNC (set_font),
                      (void *)font_select);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (font_select, font_select, "font_select");
  GLADE_HOOKUP_OBJECT_NO_REF (font_select, graph, "graph");
  GLADE_HOOKUP_OBJECT (font_select, vbox1, "vbox1");
  GLADE_HOOKUP_OBJECT (font_select, fontselection1, "fontselection1");
  GLADE_HOOKUP_OBJECT (font_select, hbuttonbox1, "hbuttonbox1");
  GLADE_HOOKUP_OBJECT (font_select, button1, "button1");
  GLADE_HOOKUP_OBJECT (font_select, button2, "button2");
  GLADE_HOOKUP_OBJECT (font_select, alignment1, "alignment1");
  GLADE_HOOKUP_OBJECT (font_select, hbox1, "hbox1");
  GLADE_HOOKUP_OBJECT (font_select, image1, "image1");
  GLADE_HOOKUP_OBJECT (font_select, label1, "label1");
  GLADE_HOOKUP_OBJECT (font_select, button3, "button3");

  return font_select;
}

void
set_font                               (GtkButton       *button,
                                        gpointer         user_data)
{
	GtkWidget *font  = (GtkWidget *)user_data;
	GtkWidget *graph = (GtkWidget *)lookup_widget(font,"graph");

	graph_options *opts = (graph_options *)lookup_widget(graph,"graph_opts");
	GnomeCanvas *canvas = (GnomeCanvas *)lookup_widget(graph,"canvasGraph");
	GraphFrame **graphptr = (GraphFrame **)lookup_widget(graph,"graphptr");

	GtkFontSelection *fontsel = (GtkFontSelection *)lookup_widget(font,"fontselection1");

	//GConfEngine *gc_engine = gconf_engine_get_default();

	opts->show_label = 1;
	//gconf_engine_set_string(gc_engine,"/apps/w3live/label_font",gtk_font_selection_get_font_name(fontsel),NULL);

	opts->label_font = gtk_font_selection_get_font(fontsel);
	gdk_font_ref(opts->label_font);

	draw_graph(canvas,*graphptr,opts);
	
	gtk_widget_destroy(font);
	//gconf_engine_unref(gc_engine);
}

void
unset_font                             (GtkButton       *button,
                                        gpointer         user_data)
{
	GtkWidget *font  = (GtkWidget *)user_data;
	GtkWidget *graph = (GtkWidget *)lookup_widget(font,"graph");
	graph_options *opts = (graph_options *)lookup_widget(graph,"graph_opts");
	GnomeCanvas *canvas = (GnomeCanvas *)lookup_widget(graph,"canvasGraph");
	GraphFrame **graphptr = (GraphFrame **)lookup_widget(graph,"graphptr");

	if(opts->label_font)
		gdk_font_unref(opts->label_font);
	
	opts->show_label = 0;
	draw_graph(canvas,*graphptr,opts);
	gtk_widget_destroy(font);
}
